//
//  NPHotSearchView.m
//  newProject
//
//  Created by Mac mini on 2017/10/13.
//  Copyright © 2017年 zt. All rights reserved.
//

#import "NPHotSearchView.h"

#define buttonH 30

@implementation NPHotSearchView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self reloadHistory];
    }
    return self;
}

-(void)click:(UIButton *)sender{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SearchNotify" object:nil userInfo:@{@"searchName":sender.titleLabel.text}];
}

-(void)reloadHistory{

    for (UIButton *button in self.subviews) {
        [button removeFromSuperview];
    }
    
    NSArray *array = [ACEPublicTool readDataFromPlist:@"searchHistory" andWillBackType:@"NSArray"];
    
    //        NSArray *array = @[@"长筒袜",@"篮球",@"哇哈哈",@"追风筝的女孩",@"矿泉水",@"书",@"打印机",@"餐饮收银机",@"笔记本",@"台式机",@"iphone X",@"iphone 8",@"电脑桌子全套",@"杯",@"儿童吃饭不用筷子之勺子",@"不烫手的锅",@"派克",@"沙漏"];
    
    CGFloat currentX = 10;
    CGFloat currentY = 0;
    
    for (int i = 0; i<array.count; i++) {
        UIButton *button = [[UIButton alloc]init];
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button sizeToFit];
        
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i>0) {
            CGFloat W = currentX+button.frame.size.width;
            if (W>self.frame.size.width) {
                currentY += 40;
                currentX = 10;
            }else{
                
            }
        }
        button.frame = CGRectMake(currentX, currentY, button.frame.size.width+10, button.frame.size.height);
        
        button.layer.borderColor = [UIColor whiteColor].CGColor;
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = button.frame.size.height/2;
        [self addSubview:button];
        
        currentX += button.frame.size.width + 15;
    }


}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
